package com.lc.zpyh.ui.fragment.home;

import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.zpyh.R;
import com.lc.zpyh.app.AppFragment;
import com.lc.zpyh.http.request.SelectMessagesApi;
import com.lc.zpyh.http.response.SelectMessageBean;
import com.lc.zpyh.other.IntentKey;
import com.lc.zpyh.ui.activity.home.ArticleDetailsActivity;
import com.lc.zpyh.ui.activity.home.FoodNewsActivity;
import com.lc.zpyh.util.SPUtil;
import com.lc.zpyh.widget.UIHelper;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class DiscounNoticeFragment  extends AppFragment<FoodNewsActivity> {
    private static final String ARG_PARAM1 = "param1";

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    // TODO: Rename and change types of parameters
    private String system_id;

    private BaseQuickAdapter<SelectMessageBean.MessagesBean, BaseViewHolder> adapter;
    List<String> dataBeanList=new ArrayList<>();
    public static DiscounNoticeFragment newInstance(String system_id) {
        DiscounNoticeFragment fragment = new DiscounNoticeFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, system_id);

        fragment.setArguments(args);
        return fragment;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            system_id = getArguments().getString(ARG_PARAM1);

        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        selectMessages(system_id);



    }
    private void selectMessages(String system_id) {
  EasyHttp.post(this)
          .api(new SelectMessagesApi().setUserid(SPUtil.get(getContext(), IntentKey.USERID, "")).setType(system_id).setPageNum("1").setPageSize("10")).request(new HttpCallback<SelectMessageBean>(this) {

        @Override
        public void onSucceed(SelectMessageBean data) {
            if (data.getCode()==0) {

                    adapter = new BaseQuickAdapter<SelectMessageBean.MessagesBean, BaseViewHolder>
                            (R.layout.item_discounnotice, data.getMessages()) {

                        @Override
                        protected void convert(BaseViewHolder baseViewHolder, SelectMessageBean.MessagesBean dataBean) {

                            baseViewHolder.setText(R.id.tv_title, dataBean.getMessageTheme());
                            baseViewHolder.setText(R.id.tv_huodong, dataBean.getNoticeContent());
                            baseViewHolder.setText(R.id.tv_date, dataBean.getSendTime());
                            baseViewHolder.getView(R.id.rl_look).setOnClickListener(view -> {
                                UIHelper.startActivity(getContext(), ArticleDetailsActivity.class);
                            });
                        }


                    };

                    View emptyview = View.inflate(getContext(), R.layout.empty_emptylayout, null);

                    adapter.setEmptyView(emptyview);
                    recyclerView.setAdapter(adapter);

            }
        }
        @Override
        public void onFail(Exception e) {
            super.onFail(e);

        }
    });
}
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_discounnotice;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }
}
